package com.coocaa.ops.plus.dashboard.v1.codegen.convert;

import com.coocaa.ops.plus.dashboard.v1.codegen.constant.ProjectConstants;
import com.coocaa.ops.plus.dashboard.v1.codegen.constant.SqlConstants;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.ColumnConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.TableInfoModel;
import org.apache.commons.collections.CollectionUtils;

import java.util.Iterator;
import java.util.List;

/**
 * @author : bijiahao
 * @date : 2019/5/10.
 * @description:<p> </p>
 */
public class ColumnListConvert {

    public static List<ColumnConfigModel> convertNotExistColumnList(List<ColumnConfigModel> commonColumnList) {
        List<ColumnConfigModel> superClassColumnList = ProjectConstants.listSuperClassColumn();
        if (CollectionUtils.isEmpty(commonColumnList)) {
            return superClassColumnList;
        }
        Iterator<ColumnConfigModel> superIt = superClassColumnList.iterator();
        while (superIt.hasNext()) {
            ColumnConfigModel columnConfigModel = superIt.next();
            if (isContain(commonColumnList, columnConfigModel.getColumnName())) {
                superIt.remove();
            }
        }
        return superClassColumnList;
    }

    private static boolean isContain(List<ColumnConfigModel> list, String columnName) {
        for (ColumnConfigModel columnConfigModel : list) {
            if (columnConfigModel.getColumnName().equals(columnName)) {
                return true;
            }
        }
        return false;
    }

    public static void filterColumns(TableInfoModel tableInfoModel) {
        List<ColumnConfigModel> list = tableInfoModel.getColumnList();
        Iterator<ColumnConfigModel> it = list.iterator();
        while (it.hasNext()) {
            ColumnConfigModel columnConfigModel = it.next();
            for (String column : SqlConstants.SUPER_ENTITY_COLUMNS_ORIGIN_TABLE) {
                if (column.equals(columnConfigModel.getColumnName())) {
                    it.remove();
                }
            }
        }
    }
}
